/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.cloudfactory.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.cloudfactory.entity.CloudfactoryFunction;
import com.thinkgem.jeesite.modules.cloudfactory.service.CloudfactoryFunctionService;

/**
 * 单表生成Controller
 * @author ThinkGem
 * @version 2018-09-08
 */
@Controller
@RequestMapping(value = "${adminPath}/cloudfactory/cloudfactoryFunction")
public class CloudfactoryFunctionController extends BaseController {

	@Autowired
	private CloudfactoryFunctionService cloudfactoryFunctionService;
	
	@ModelAttribute
	public CloudfactoryFunction get(@RequestParam(required=false) String id) {
		CloudfactoryFunction entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = cloudfactoryFunctionService.get(id);
		}
		if (entity == null){
			entity = new CloudfactoryFunction();
		}
		return entity;
	}
	
	@RequiresPermissions("cloudfactory:cloudfactoryFunction:view")
	@RequestMapping(value = {"list", ""})
	public String list(CloudfactoryFunction cloudfactoryFunction, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CloudfactoryFunction> page = cloudfactoryFunctionService.findPage(new Page<CloudfactoryFunction>(request, response), cloudfactoryFunction); 
		model.addAttribute("page", page);
		return "cloudfactory/cloudfactoryFunctionList";
	}

	@RequiresPermissions("cloudfactory:cloudfactoryFunction:view")
	@RequestMapping(value = "form")
	public String form(CloudfactoryFunction cloudfactoryFunction, Model model) {
		model.addAttribute("cloudfactoryFunction", cloudfactoryFunction);
		return "cloudfactory/cloudfactoryFunctionForm";
	}

	@RequiresPermissions("cloudfactory:cloudfactoryFunction:edit")
	@RequestMapping(value = "save")
	public String save(CloudfactoryFunction cloudfactoryFunction, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, cloudfactoryFunction)){
			return form(cloudfactoryFunction, model);
		}
		if(cloudfactoryFunction.getBasics()==null) {
			cloudfactoryFunction.setBasics("0");
		}
		if(cloudfactoryFunction.getAdvanced()==null) {
			cloudfactoryFunction.setAdvanced("0");
		}
		if(cloudfactoryFunction.getProfessional()==null) {
			cloudfactoryFunction.setProfessional("0");
		}
		cloudfactoryFunctionService.save(cloudfactoryFunction);
		addMessage(redirectAttributes, "保存单表成功");
		return "redirect:"+Global.getAdminPath()+"/cloudfactory/cloudfactoryFunction/?repage";
	}
	
	@RequiresPermissions("cloudfactory:cloudfactoryFunction:edit")
	@RequestMapping(value = "delete")
	public String delete(CloudfactoryFunction cloudfactoryFunction, RedirectAttributes redirectAttributes) {
		cloudfactoryFunctionService.delete(cloudfactoryFunction);
		addMessage(redirectAttributes, "删除单表成功");
		return "redirect:"+Global.getAdminPath()+"/cloudfactory/cloudfactoryFunction/?repage";
	}

}